package com.luobd.auth;


import com.luobd.base.ResponseData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@Component
public class AuthenticationFailHandler implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        String remoteHost = httpServletRequest.getRemoteHost();
        log.warn("远程主机{}认证失败，访问拒绝>>>>>>>>>", remoteHost);
        httpServletResponse.setStatus(HttpServletResponse.SC_OK);
        ResponseData<?> responseData = ResponseData.notAuthorized("Not  Authentication");
        httpServletResponse.getWriter().write(responseData.toJson());
    }
}
